//
//  HmSelectAdView.m
//  AiaiWang
//
//  Created by 赵海明 on 2018/3/28.
//  Copyright © 2018年 cnmobi. All rights reserved.
//

#import "HmSelectAdView.h"
#import "UIView+Extension.h"

#define kPickerViewHeight 200
#define kTitleHeight 30
/*** RGB颜色 */
#define HmColorRGB(r, g, b) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1.0]
/*** RGBA颜色 */
#define HmColorRGBA(r, g, b, a) [UIColor colorWithRed:(r) / 255.f green:(g) / 255.f blue:(b) / 255.f alpha:(a)]
// 屏幕的width
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
// 屏幕的height
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface HmSelectAdView()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *pickerV;
@property (nonatomic, strong) NSMutableArray *allDataArr;
@property (nonatomic, strong) NSMutableArray *provinceArr;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) NSMutableArray *areaArr;

@property (nonatomic, strong) NSString *currentSelectProvince;

@property (nonatomic, strong) NSString *AreaID;
@property (nonatomic, strong) NSString *currentSelectCity;
@property (nonatomic, strong) NSString *currentSelectArea;

// 布局控件
@property (nonatomic, strong) UIButton *bgV;

@end

@implementation HmSelectAdView

- (instancetype)initWithLastContent:(NSArray *)lastContent {
    if ([super init]) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        if (lastContent) {
            self.currentSelectProvince = lastContent.firstObject;
            self.currentSelectCity = lastContent[1];
            self.currentSelectArea = lastContent.lastObject;
        }
        
        [self setupView];
    }
    return self;
}

- (void)show {
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

#pragma mark -- UIPickerView Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.allDataArr.count;
    }else if (component == 1) {
        return self.cityArr.count;
    }else {
        return self.areaArr.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.allDataArr[row];
//    }else if (component == 1) {
//        return self.cityArr[row];
//    }else {
//        return self.areaArr[row];
//    }
    return @"dsfdsfdsf";
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 30;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.currentSelectProvince = _allDataArr[row];
        
        SeleAddressModel *moide = self.allDataArr[row];
        self.cityArr = moide.city;
  
        //self.confirmSelect(@[self.AreaID, self.currentSelectCity, self.currentSelectArea]);
        
        SeleAddressModel *moide3 = self.cityArr[0];
        self.areaArr =moide3.area;
        self.currentSelectCity = moide3.area_name;
        
        SeleAddressModel *moide2 = self.areaArr[0];
        self.AreaID = moide2.Id;
        self.currentSelectArea = moide2.area_name;

        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    } else if (component == 1) {
        SeleAddressModel *moide = self.cityArr[row];
        self.currentSelectCity = moide.area_name;
        self.areaArr = moide.area;
        
        SeleAddressModel *moide2 = self.areaArr[0];
        self.AreaID = moide2.Id;
        self.currentSelectArea = moide2.area_name;
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    } else {
        SeleAddressModel *moide = self.areaArr[row];
        self.currentSelectArea = moide.area_name;
        self.AreaID = moide.Id;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    if (!view) {
        view = [[UIView alloc] init];
    }
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (kScreenWidth - 50) / 3, 30)];
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.font = [UIFont systemFontOfSize:15];
    lblTitle.textColor = HmColorRGB(51, 51, 51);
    if (component == 0) {
        SeleAddressModel *moide = self.allDataArr[row];
        lblTitle.text =moide.area_name;
    }else if (component == 1) {
//        if (row<0) {
//            row = 0;//莫名其妙会崩
//        }
        SeleAddressModel *moide = self.cityArr[row];
        lblTitle.text = moide.area_name;
    }else {
        SeleAddressModel *moide = self.areaArr[row];
        lblTitle.text =[NSString stringWithFormat:@"%@",moide.area_name];
    }
    [view addSubview:lblTitle];
    return view;
}

#pragma mark -- Functions
/// 设置view
- (void)setupView {
    // 背景
    self.bgV = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _bgV.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    _bgV.backgroundColor = HmColorRGBA(0, 0, 0, 0.4);
    [_bgV setTitle:@"" forState:(UIControlStateNormal)];
    [_bgV addTarget:self action:@selector(cancelAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:_bgV];
    // 承载view
    UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(20, (kScreenHeight - kPickerViewHeight - 80) / 2, kScreenWidth - 40, kPickerViewHeight + 80)];
    vv.layer.cornerRadius = 10;
    vv.layer.masksToBounds = YES;
    vv.backgroundColor = [UIColor whiteColor];
    [self addSubview:vv];
    // title
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, vv.hm_width, kTitleHeight)];
    lblTitle.textColor = HmColorRGB(51, 51, 51);
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.text = @"选择地区";
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.font = [UIFont systemFontOfSize:14];
    [vv addSubview:lblTitle];
    // PickerView
    self.pickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(0, lblTitle.hm_bottom, vv.hm_width, kPickerViewHeight)];
    self.pickerV.frame = CGRectMake(0, lblTitle.hm_bottom, vv.hm_width, kPickerViewHeight);
    self.pickerV.delegate = self;
    self.pickerV.dataSource = self;
    self.pickerV.backgroundColor = [UIColor clearColor];
    [vv addSubview:_pickerV];
    // 分割线(横)
    UILabel *lblLineH = [[UILabel alloc] initWithFrame:CGRectMake(0, _pickerV.hm_bottom, vv.hm_width, 1)];
    lblLineH.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [vv addSubview:lblLineH];
    // 取消
    UIButton *btnCancel = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btnCancel.frame = CGRectMake(0, lblLineH.hm_bottom, vv.hm_width / 2 - 0.5, vv.hm_height - lblLineH.hm_bottom);
    btnCancel.backgroundColor = [UIColor clearColor];
    [btnCancel setTitle:@"取消" forState:(UIControlStateNormal)];
    [btnCancel setTitleColor:HmColorRGB(102, 102, 102) forState:(UIControlStateNormal)];
    [btnCancel addTarget:self action:@selector(cancelAction:) forControlEvents:(UIControlEventTouchUpInside)];
    btnCancel.titleLabel.font = [UIFont systemFontOfSize:15];
    [vv addSubview:btnCancel];
    // 分割线(竖)
    UILabel *lblLineV = [[UILabel alloc] initWithFrame:CGRectMake(btnCancel.hm_right, lblLineH.hm_bottom, 1, btnCancel.hm_height)];
    lblLineV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [vv addSubview:lblLineV];
    // 确定
    UIButton *btnSure = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btnSure.frame = CGRectMake(lblLineV.hm_right, lblLineH.hm_bottom, vv.hm_width - lblLineV.hm_right, btnCancel.hm_height);
    btnSure.backgroundColor = [UIColor clearColor];
    [btnSure setTitle:@"确定" forState:(UIControlStateNormal)];
    [btnSure setTitleColor:HmColorRGB(83, 184, 255) forState:(UIControlStateNormal)];
    [btnSure addTarget:self action:@selector(sureAction:) forControlEvents:(UIControlEventTouchUpInside)];
    btnSure.titleLabel.font = [UIFont systemFontOfSize:15];
    [vv addSubview:btnSure];
    
    [self HmGetArea];

}

/// 取消
- (void)cancelAction:(UIButton *)btn {
    [self removeFromSuperview];
}

/// 确定
- (void)sureAction:(UIButton *)btn {
    if (self.confirmSelect) {
        self.confirmSelect(@[self.AreaID, self.currentSelectCity, self.currentSelectArea]);
    }
    [self removeFromSuperview];
}

/// 解析地址
- (void)HmGetArea {
    self.allDataArr = [NSMutableArray arrayWithCapacity:1];

    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [NetWorkTool POST:getManageAreast param:mudic success:^(id dic) {
        [self.allDataArr removeAllObjects];
        
        KMyLog(@"dfdsdsfsdfds%@",[HFTools toJSONString:dic]);
        for (NSDictionary *mydic in [[dic objectForKey:@"data"] objectForKey:@"list1"]) {
            SeleAddressModel *model = [[SeleAddressModel alloc]init];
            [model setValuesForKeysWithDictionary:mydic];
            NSMutableArray *shiMu = [NSMutableArray arrayWithCapacity:1];
            for (NSDictionary *shi in model.city) {
                SeleAddressModel *shiModel = [[SeleAddressModel alloc]init];
                [shiModel setValuesForKeysWithDictionary:shi];
                [shiMu addObject:shiModel];
                NSMutableArray *quMu = [NSMutableArray arrayWithCapacity:1];
                for (NSDictionary *qu in shiModel.area) {
                    SeleAddressModel *quModel = [[SeleAddressModel alloc]init];
                    [quModel setValuesForKeysWithDictionary:qu];
                    [quMu addObject:quModel];
                }
                shiModel.area =quMu;
            }
            model.city =shiMu;
            [self.allDataArr addObject:model];
        }
        
        [self.pickerV selectRow:0 inComponent:0 animated:YES];
    
        SeleAddressModel *moide = self.allDataArr[0];
        self.cityArr = moide.city;
        self.currentSelectCity = moide.area_name;
        [self.pickerV selectRow:0 inComponent:1 animated:YES];
        
        
        SeleAddressModel *moide2 = self.cityArr[0];
        self.areaArr = moide2.area;
        [self.pickerV selectRow:0 inComponent:2 animated:YES];
        self.AreaID = moide2.Id;
        self.currentSelectArea = moide2.area_name;
        [self.pickerV reloadAllComponents];
        

        SeleAddressModel *moide05 = self.allDataArr[0];
        self.cityArr = moide05.city;
        SeleAddressModel *moide30 = self.cityArr[0];
        self.areaArr =moide30.area;
        self.currentSelectCity =moide30.area_name;
        SeleAddressModel *moide20 = self.areaArr[0];
        self.AreaID = moide20.Id;
        self.currentSelectArea =moide20.area_name;
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
//    [self removeAllObjectFromArea];
//    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aiai_area.txt" ofType:nil]];
//    if (!data) {
//        return;
//    }
//    NSArray *allArr = [HmAddressModel arrayOfModelsFromData:data error:nil];
//    self.allDataArr = [NSArray arrayWithArray:allArr];
//    NSLog(@"dsfdsfsd%@",allArr);
//    [self calculationCityAreaArr];
//    [self.pickerV selectRow:[self.provinceArr indexOfObject:self.currentSelectProvince] inComponent:0 animated:YES];
//    [self.pickerV selectRow:[self.cityArr indexOfObject:self.currentSelectCity] inComponent:1 animated:YES];
//    [self.pickerV selectRow:[self.areaArr indexOfObject:self.currentSelectArea] inComponent:2 animated:YES];
}

/// 清空当前数据
- (void)removeAllObjectFromArea {
    [self.provinceArr removeAllObjects];
    [self.cityArr removeAllObjects];
    [self.areaArr removeAllObjects];
}


#pragma mark -- Getter
- (NSMutableArray *)provinceArr {
    if (!_provinceArr) {
        _provinceArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _provinceArr;
}

- (NSMutableArray *)cityArr {
    if (!_cityArr) {
        _cityArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _cityArr;
}

- (NSMutableArray *)areaArr {
    if (!_areaArr) {
        _areaArr = [NSMutableArray arrayWithCapacity:1];
    }
    return _areaArr;
}

#pragma mark -- Setter

@end
